package com.example.library.adapter;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.example.library.fragment.NewClassicFragment;
import com.example.library.fragment.NewCommonCollectionFragment;
import com.example.library.fragment.NewDifferentiationFragment;
import com.example.library.fragment.NewKnowledgeParseFragment;
import com.example.library.viewmodle.CheckWordDialogViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dadong on 2018/8/28.
 * Describe:
 */
public class KnowledgePageAdapter extends FragmentPagerAdapter {
    int num;
    private HashMap<Integer, Fragment> mFragmentHashMap = new HashMap<>();
    private List<String> titles;
    CheckWordDialogViewModel mViewModel;

    public KnowledgePageAdapter(FragmentManager fm, CheckWordDialogViewModel mViewModel) {
        super(fm);
        num = 0;
        titles = new ArrayList<>();
        this.mViewModel = mViewModel;
        dealNum();
    }

    private void dealNum() {
        if (mViewModel.senCollectionBeans != null && mViewModel.senCollectionBeans.size() > 0) {
            titles.add("释义例句");
            num++;
        }
        if (mViewModel.collSenCollectionBeans != null && mViewModel.collSenCollectionBeans.size() > 0) {
            titles.add("常用搭配");
            num++;
        }
        if (mViewModel.discCollectionBeans != null && mViewModel.discCollectionBeans.size() > 0) {
            titles.add("辨析");
            num++;
        }
        if (mViewModel.classCollectionBeans != null && mViewModel.classCollectionBeans.size() > 0) {
            titles.add("经典例句");
            num++;
        }
    }


    @Override
    public Fragment getItem(int position) {
        return createFragment(position);
    }

    @Override
    public int getCount() {
        return num;
    }

    private Fragment createFragment(int position) {
        Fragment fragment = mFragmentHashMap.get(position);
        if (fragment == null) {
            if (titles.get(position).equals("释义例句")) {
                NewKnowledgeParseFragment knowledgeParseFragment = new NewKnowledgeParseFragment();
                knowledgeParseFragment.setParaphraseBeans(mViewModel.senCollectionBeans);
                fragment = knowledgeParseFragment;
            } else if (titles.get(position).equals("常用搭配")) {
                NewCommonCollectionFragment commonCollectionFragment = new NewCommonCollectionFragment();
                commonCollectionFragment.setCollectionBeans(mViewModel.collSenCollectionBeans);
                fragment = commonCollectionFragment;
            } else if (titles.get(position).equals("辨析")) {
                NewDifferentiationFragment differentiationFragment = new NewDifferentiationFragment();
                differentiationFragment.setDifferentiationBeans(mViewModel.discCollectionBeans);
                fragment = differentiationFragment;
            } else if (titles.get(position).equals("经典例句")) {
                NewClassicFragment classicFragment = new NewClassicFragment();
                classicFragment.setClassCollectionBeans(mViewModel.classCollectionBeans);
                fragment = classicFragment;
            }
            mFragmentHashMap.put(position, fragment);
        }
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}
